package cn.tedu.order.service;

import cn.tedu.order.entity.Order;
import cn.tedu.order.feign.AccountClient;
import cn.tedu.order.feign.EasyIdClient;
import cn.tedu.order.feign.StorageClient;
import cn.tedu.order.mapper.OrderMapper;
import cn.tedu.order.tcc.OrderTccAction;
import com.google.inject.internal.cglib.core.$Customizer;
import io.seata.spring.annotation.GlobalTransactional;
import org.checkerframework.checker.index.qual.GTENegativeOne;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private StorageClient storageClient;
    @Autowired
    private AccountClient accountClient;
    @Autowired
    private EasyIdClient easyIdClient;
    @Autowired
    private OrderTccAction orderTccAction;
    @GlobalTransactional
    @Override
    public void create(Order order) {
        // TODO: 从全局唯一id发号器获得id，这里暂时随机产生一个 orderId
        String s = easyIdClient.nextId("order_business");
        Long orderId = Long.valueOf(s);
        order.setId(orderId);
        //orderMapper.create(order);
        /**
         * orderTccAction动态代理对象，有拦截器
         * 拦截器中会判断如果没有第一个参数，会创建上下文对象，在传入原始方法
         */
        orderTccAction.prepareCreatOrder(null,order.getId(),order.getUserId(),order.getProductId(),order.getCount(),order.getMoney());
        // TODO: 调用storage，修改库存
        //storageClient.decrease(order.getProductId(), order.getCount());
        // TODO: 调用account，修改账户余额
        //accountClient.decrease(order.getUserId(), order.getMoney());

    }
}
